<template>
	<view class="uni-page">
		<view class="buy-btn">
			<button type="primary" size="mini" @click="buyToken">购买token</button>
		</view>
		<uni-table border stripe emptyText="暂无更多数据">
			<!-- 表头行 -->
			<uni-tr>
				<uni-th align="center">ID</uni-th>
				<uni-th align="center">购买数量</uni-th>
				<uni-th width="200" align="center">时间</uni-th>
				<uni-th align="left">状态</uni-th>
				<uni-th>操作</uni-th>
			</uni-tr>
			<!-- 表格数据行 -->
			<uni-tr v-for="item in tokenList" :key="item.id">
				<uni-td>{{item.id}}</uni-td>
				<uni-td>{{item.num}}</uni-td>
				<uni-td>{{item.createtime}}</uni-td>
				<uni-th v-if="item.status==0">正在购买</uni-th>
				<uni-th v-if="item.status==1">已购买</uni-th>
				<uni-th v-if="item.status>1">拒绝</uni-th>
				<uni-td>
					<button class="uni-button" size="mini"
						:type="item.status == 0 ? 'info' : (item.status == 1 ? 'primary' : 'danger')"
						:disabled="item.status == 0 ? true : (item.status == 1 ? false : true)"
						@click="deriveToken(item)">导出</button>
				</uni-td>
			</uni-tr>

		</uni-table>

		<view class="uni-pagination-box"><uni-pagination show-icon :page-size="pageSize" :current="pageCurrent"
				:total="total" @change="change" /></view>

		<uni-popup ref="tokenDialog" type="dialog">
			<uni-popup-dialog ref="inputClose" mode="input" title="提示" placeholder="请输入要购买的数量"
				@confirm="tokenInputConfirm">
			</uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script setup>
	import {
		onMounted,
		ref
	} from 'vue';
	import {
		getTokenAPI,
		exportExcel,
		buyTokenAPI
	} from '../../apis/token';
	import {
		useSettingStore
	} from '../../stores';

	const tokenList = ref([])
	const pageSize = 10
	const pageCurrent = ref(1)
	const total = ref(0)
	const tokenDialog = ref()
	const buyToken = () => {
		tokenDialog.value.open()
	}
	const settingStore = useSettingStore()
	const settings = settingStore.settings
	const tokenInputConfirm = async (e) => {

		uni.showModal({
			title: '提示',
			content: `本次购买token需要支付：${e*settings.tokenjg}`,
			success: async function(res) {
				if (res.confirm) {
					const {
						code
					} = await buyTokenAPI({
						num: e
					})
					if (code == 1) {
						uni.showToast({
							title: '购买成功'
						})
						getList()
					}

				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		})
	}
	const getList = async () => {
		const res = await getTokenAPI()
		if (res.code == 1) {
			tokenList.value = res.data.sqlist
			total.value = res.data.zongshu
		};
	}
	onMounted(() => {
		getList()

	})
	const change = (e) => {
		pageCurrent.value = e.current
		pageSize.value = e.size
	}
	const deriveToken = (async row => {
		const res = await exportExcel(row)
		// console.log('文件', res);
		// 文件到本地
		uni.saveFile({
			tempFilePath: res, //临时路径
			success: function(data) {
				console.log(data);
				var savedFilePath = data.savedFilePath
				console.log(savedFilePath);
				// 在app端执行
				// #ifdef APP-PLUS
				let osname = plus.os.name
				console.log(osname);
				// 如果是安卓的话弹出提示
				if (osname == 'Android') {
					uni.showModal({
						title: '提示',
						content: '文件已保存：' + data.savedFilePath,
						cancelText: '我知道了',
						confirmText: '打开文件',
						success: function(res) {
							if (res.confirm) {
								uni.openDocument({
									filePath: data.savedFilePath,
									success: (sus) => {
										console.log('成功打开');
									}
								})
							}
						}
					});
				}
				// #endif

			}
		})
		console.log('下载成功xx');
	})
</script>

<style>
	.uni-page {
		padding: 20upx;
	}
</style>